<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
//展示页
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array();
	//查询
	//代理商id
     if(!empty($_RQ['uniacid'])){
		$params['uniacid']=trim($_RQ['uniacid']);
		$return['params']['uniacid']=$params['uniacid'];
	}
	//产生时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	else{
		$return['params']['createtime']=array();
	}
	
	//结算单号
	if(!empty($_RQ['clearsn'])){
		$params['clearsn']=trim($_RQ['clearsn']);
		$return['params']['clearsn']=$params['clearsn'];
	}
	$fields=array('id','uniacid','order_id','money','status','clearsn','createtime');
    $orderby='id desc';
	$total='';
	//$parameter=array('params'=>$params,'limit'=>array($pindex,$psize),'fields'=>array('id','uniacid','order_id','money','status','clearsn','createtime'));
	$list = pdo_getslice('core_agent_commission',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if($list){
		foreach($list as &$item){

		}
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['list']=$list?$list:array();
	$htmls=array(array('type' =>'list','tabs'=>array(
			array('op'=>'list','title'=>'佣金管理'),
			),
			'list' =>array ('data' => 'list','list' => array (0 => array (
		        array('title' => 'id','name' => 'id','type'=>'string',),),
		        array ( array ('title' => '代理商ID','name' =>'uniacid','type' => 'string',),),
		         array (array ('title' => '关联订单号','name'=>'order_id','type' => 'string',),),
				array(array('title' => '佣金金额','name' => 'money','type' => 'string',),),
		         array (array ('title' => '佣金状态','name' => 'status','type' => 'radio','options' => 'status_xuanxiang',), ),
				 array (array ('title' => '结算单号','name' => 'clearsn','type' => 'string', ),),
				array (array ('title' => '佣金产生时间','name' => 'createtime','type' => 'time', ),),
				),
			   'delall' => true,
		       'del_one' => true,
		       'edit' => false,
		       'action' => 
		    array (
			   array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>'0')),
				   ),
		          ),
		'search' => array(
		  array ( 'type'=>'string','width'=>15,'title'=>'代理商ID','name' => 'uniacid'),
		  array ( 'type'=>'time_range','width'=>15,'title'=>'产生时间','name' => 'createtime'),
		  array ( 'type'=>'string','width'=>15,'title'=>'结算单号','name' => 'clearsn'),
		  ),
		   ),
	     );
	$return['htmls']=$htmls;
	$return['status_xuanxiang']=array (0 => '待审核',1 => '待结算',2=>'已结算','3'=>'审核不通过');
	exi($return);
}
if($_RQ['op']=='del'){
	
	$id=intval($_RQ['id']);

	if($id){
		pdo_delete('core_agent_commission',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_agent_commission',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
  }
if($_RQ['op']=='check'){
	//print_r($_RQ);exit();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_agent_commission',array('id'=>$id));
	}else{
		exi('错误id','error');
	}
    $return['item']=$item;
	$return['check']=array('1'=>'审核通过','3'=>'审核不通过');
	$htmls=array(
		'type'=>'edit',
		'post'=>'check2',
		'desc'=>'代理商审核',
		'edit'=>array(
			'1'=>array('name'=>'status','title'=>'审核','type'=>'radio','options'=>'check'),
			)
			);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='check2'){
	$postdata=get_postdata();
	$data=array_fields(array('status'),$postdata);
	$id=intval($postdata['id']);
	//$data['checktime']=TIMESTAMP;
	if($id){
		pdo_update('core_agent_commission',$data,array('id'=>$id));
		exi('设置成功！');
	}
	else{
		exi('设置无效','error');
	}
}